package com.zzyl.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.service.WechatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class WechatServiceImpl implements WechatService {

    @Value("${zzyl.wechat.appid}")
    private String appid;
    @Value("${zzyl.wechat.appSecret}")
    private String secret;
    private String openid_url = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";

    private String access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET";

    private String phone_url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN";
    @Override
    public String getOpenid(String code) {
        try {
            String openidUrlTemp = openid_url.replace("APPID", appid).replace("SECRET", secret).replace("JSCODE", code);
            String result = HttpUtil.get(openidUrlTemp);
            log.info("获取微信openid的结果为:{}", result);
            /*
                {
                "openid":"xxxxxx",
                "session_key":"xxxxx",
                "unionid":"xxxxx",
                "errcode":0,
                "errmsg":"xxxxx"
                }
             */
            JSONObject jsonObject = JSONUtil.parseObj(result);
            if(jsonObject.get("error_code")!=null){
                throw new BaseException(BasicEnum.GET_OPENID_ERROR);
            }
            return jsonObject.getStr("openid");
        }catch (Exception e){
            throw new BaseException(BasicEnum.GET_OPENID_ERROR);
        }
    }

    @Override
    public String getPhone(String code) {
log.info("获取手机号参数为:{}", code);
        try {
            //1、获取access_token
            String accessTokenUrlTemp = access_token_url.replace("APPID", appid).replace("SECRET", secret);
            String result = HttpUtil.get(accessTokenUrlTemp);
            log.info("获取微信access_token的结果为:{}", result);
            /*
                {
                "access_token":"ACCESS_TOKEN",
                "expires_in":7200
                }
             */
            JSONObject jsonObject = JSONUtil.parseObj(result);
            if(jsonObject.get("error_code")!=null){
                throw new BaseException(BasicEnum.GET_TOKEN_ERROR);
            }
            String accessToken = jsonObject.getStr("access_token");

            //2、获取手机号
            String phoneUrlTemp = phone_url.replace("ACCESS_TOKEN", accessToken);
            Map<String,String> map = new HashMap<>();
            map.put("code", code);

            result = HttpUtil.post(phoneUrlTemp, JSONUtil.toJsonStr(map));
            log.info("获取微信绑定的手机号的结果为:{}", result);
            jsonObject = JSONUtil.parseObj(result);
            if(jsonObject.get("error_code")!=null){
                throw new BaseException(BasicEnum.GET_PHONE_ERROR);
            }
            return jsonObject.getJSONObject("phone_info").getStr("phoneNumber");
        }catch (Exception e){
            throw new BaseException(BasicEnum.GET_PHONE_ERROR);
        }
    }

}
